const path = require('path');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const {GitRevisionPlugin} = require("git-revision-webpack-plugin");
const {SetGitInfoPlugin} = require("./set-git-info-plugin");
const {FileListPlugin} = require("./file-list-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const webpack = require("webpack");

const gitRevisionPlugin = new GitRevisionPlugin()

module.exports = {
    entry: path.resolve(__dirname, './src/index.js'),
    output: {
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, 'dist'),
        clean: true, // https://webpack.docschina.org/configuration/output/#outputclean
    },
    mode: 'production',
    // 开发服务器配置
    // https://webpack.docschina.org/configuration/dev-server/#devserver
    devServer: {
        contentBase: path.join(__dirname, 'dist'),
        compress: true,
        port: 2021,
    },
    optimization: {
        splitChunks: {
            chunks: 'all',
        },
    },
    // 文件监听配置
    // https://webpack.docschina.org/configuration/watch/#watch
    watchOptions: {
        aggregateTimeout: 600,
        ignored: /node_modules/,
    },
    plugins: [
        new HtmlWebpackPlugin({
            branch: gitRevisionPlugin.branch(),
            lastCommit: gitRevisionPlugin.commithash(),
            template: "index.html"
        }),
        new SetGitInfoPlugin(),
        new FileListPlugin(),
        // 拷贝 资源文件
        // https://webpack.docschina.org/plugins/copy-webpack-plugin/#getting-started
        // new CopyPlugin({
        //     patterns: [
        //         { from: "public", to: path.resolve(__dirname, './dist') },
        //     ],
        // }),
        new webpack.DefinePlugin({
            BRANCH: JSON.stringify(gitRevisionPlugin.branch())
        })
    ]
};
